﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Diagnostics;

namespace Confessor
{
    static class xWMIActions
    {
        // Verify WMI Connectivity.
        static public string verifyWMIConnectivity(string targetHost, string userName, string password)
        {
            ConnectionOptions oConn = new ConnectionOptions();
            oConn.Username = userName;
            oConn.Password = password;

            // Set up the WMI Management Objects
            ManagementScope mScope = new ManagementScope(@"\\" + targetHost + @"\root\cimv2", oConn);
            ManagementClass mClass = new ManagementClass(mScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
            object[] command = { "testConnectivity" };

            try
            {
                mClass.InvokeMethod("Create", command);
                return "true";
            }
            catch (Exception ex)
            {
                // Will need to set up error handling at the caller level
                // string statusMessage = targetHost + " " + ex.Message;
                // xFA.WriteToErrorLog(statusMessage, scanObject);
                return ex.Message;
            }
        }

        // Set the variable targetHostName
        static public string returnTargetHostname(string targetHost, string userName, string password)
        {
            string hostName = string.Empty;

            ConnectionOptions oConn = new ConnectionOptions();
            oConn.Username = userName;
            oConn.Password = password;

            // Set up the WMI Management Objects
            ManagementScope mScope = new ManagementScope(@"\\" + targetHost + @"\root\cimv2", oConn);
            ManagementClass mClass = new ManagementClass(mScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());

            // Set the variable TargetHostName
            SelectQuery qryHostName = new SelectQuery("Win32_ComputerSystem");
            ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mScope, qryHostName);

            foreach (ManagementBaseObject compVar in mSearcher.Get())
            {
                hostName = compVar["Name"].ToString();
                if (hostName == string.Empty)
                {
                    hostName = targetHost;
                }
            }
            return hostName;
        }

        // Function to start a process on a remote system.
        static public string startRemoteProcess(string targetHost, string userName, string password, string commandStr)
        {

            ConnectionOptions oConn = new ConnectionOptions();
            oConn.Username = userName;
            oConn.Password = password;

            // Set up the WMI Management Objects
            ManagementScope mScope = new ManagementScope(@"\\" + targetHost + @"\root\cimv2", oConn);
            ManagementClass mClass = new ManagementClass(mScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());

            object[] command = { commandStr };

            try
            {
                // Create the process that will execute the batch script.
                mClass.InvokeMethod("Create", command);
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        // Get Process ID's of Remote Host
        static public void getProcessId(string userName, string password, string targetHost)
        {
            ConnectionOptions oConn = new ConnectionOptions();
            oConn.Username = userName;
            oConn.Password = password;

            // Set up the WMI Management Objects
            ManagementScope mScope = new ManagementScope(@"\\" + targetHost + @"\root\cimv2", oConn);
            ManagementClass mClass = new ManagementClass(mScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());

            // Build a query for enumeration of Drives and Network Drives
            SelectQuery query = new SelectQuery("Win32_Process");

            // Instantiate an object searcher with this query. (Scope is the management scope package)
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(mScope, query);
            foreach (ManagementBaseObject procVar in searcher.Get())
            {
                string processName = procVar["Name"].ToString();
            }
        }
    }
}

